Imports System
Imports System.Text
Imports System.Security.Cryptography

''' <summary>
''' Hash algorithms.
''' </summary>
Public Class Hash

#Region "SHA"

	''' <summary>
	''' Computes the SHA1 hash for the specified value, returns as hexadecimal.
	''' </summary>
	''' <returns>The hash result as string.</returns>
	Public Shared Function SHA1(ByVal value As String) As String

		Return Hash(New SHA1Managed, value)

	End Function

	''' <summary>
	''' Computes the SHA256 hash for the specified value, returns as hexadecimal.
	''' </summary>
	''' <returns>The hash result as string.</returns>
	Public Shared Function SHA256(ByVal value As String) As String

		Return Hash(New SHA1Managed, value)

	End Function

	''' <summary>
	''' Computes the SHA384 hash for the specified value, returns as hexadecimal.
	''' </summary>
	''' <returns>The hash result as string.</returns>
	Public Shared Function SHA384(ByVal value As String) As String

		Return Hash(New SHA384Managed, value)

	End Function

	''' <summary>
	''' Computes the SHA512 hash for the specified value.
	''' </summary>
	''' <returns>The hash result as string.</returns>
	Public Shared Function SHA512(ByVal value As String) As String

		Return Hash(New SHA512Managed, value)

	End Function

#End Region

#Region "MD5"

	''' <summary>
	''' Computes the MD5 hash for the specified value.
	''' </summary>
	''' <returns>The hash result as string.</returns>
	Public Shared Function MD5(ByVal value As String) As String

		Return Hash(New MD5CryptoServiceProvider, value)

	End Function

#End Region

#Region "Hash internal"

	Private Shared Function Hash(ByVal objHash As HashAlgorithm, ByVal value As String) As String

		Dim strReturn As String = String.Empty
		Dim objStringBuilder As New StringBuilder
		Dim arrByte() As Byte
		Dim arrHash() As Byte

		If value.Length > 0 Then

			arrByte = Config.Encoding.GetBytes(value)
			arrHash = objHash.ComputeHash(arrByte)

			Return BitConverter.ToString(arrHash).Replace("-", String.Empty)

		End If

		Return strReturn

	End Function

#End Region

End Class